package com.hoperun.checkcloth.repository

import com.google.gson.JsonObject
import com.hoperun.checkcloth.common.ClothCheckApp
import com.hoperun.checkcloth.entity.LeftOverBoltInfo
import com.hoperun.checkcloth.net.ApiResult
import com.hoperun.checkcloth.net.ClothFlawInfo
import com.hoperun.checkcloth.net.DictItem
import com.hoperun.checkcloth.net.SoapApi
import com.hoperun.checkcloth.util.SharedPreUtil
import java.text.SimpleDateFormat
import java.util.*

object Repository {
    private val api: SoapApi = SoapApi()
    var leftOverSystemInfo: LeftOverSystemInfo? = null
        private set

    /***
     *  账号密码登录
     */
    suspend fun accountPasswordLogin(account: String, password: String): Boolean {
        leftOverSystemInfo = null

        api.leftOverBoltLogin(account, password)?.let { (name, bean) ->
            leftOverSystemInfo = LeftOverSystemInfo(name, bean, name)
            // 兼容旧的代码中存储信息方式
            SharedPreUtil.putValue(ClothCheckApp.appContext, SharedPreUtil.USERBEAN, bean.toString())
            SharedPreUtil.putValue(ClothCheckApp.appContext, SharedPreUtil.XM, name)
            return true
        }
        return false
    }

    suspend fun requestLeftOverBoltInformation(monadNo: String): ApiResult<Triple<String, List<ClothFlawInfo>?, LeftOverBoltInfo?>?> {
        val result = api.leftOverBoltQueryByClothNo(monadNo)

        // 把之前登录获得的信息填入响应的字段
        // 如果响应的字段不为 null，则填入后返回
        result.data?.third?.copy(
                empId = leftOverSystemInfo?.staffNo ?: "",
                clothWorker = leftOverSystemInfo?.account ?: ""
        )?.let {
            return result.copy(data = result.data.copy(third = it))
        }

        return result
    }

    suspend fun commitLeftOverBoltFlawInformation(monadNo: String, flawList: List<ClothFlawInfo>): Boolean {
        val bean = leftOverSystemInfo?.userBean ?: return false

        return api.leftOverBoltCommitFlawInfo(bean, monadNo, flawList)
    }

    suspend fun requestNewUpgrade(): Triple<Boolean, String, String>? {
        api.queryNewVersion()?.let { info ->
            return Triple(info.forceUpgrade == "1", info.versionCode, info.url)
        }
        return null
    }

    /***
     *
     */
    suspend fun requestLeftOverBoltDataDict(): Pair<List<DictItem>, List<DictItem>>? {
        val dictId = "BADLX"
        return api.queryDataDict(dictId)
    }

    /***
     *  获得新的 apk 下载地址
     *
     *  @param urlPath 服务器响应中的 url，是去掉了 ip 地址剩下的部分
     */
    suspend fun getApkDownloadInfo(urlPath: String): String? {
        if (urlPath.isEmpty()) return null
        val date = SimpleDateFormat(
                "yyyy-MM-dd",
                Locale.getDefault()
        ).format(Date(System.currentTimeMillis()))

        return "${ClothCheckApp.appContext.getExternalFilesDir(null)}/newApk_${date}.apk"
    }

}

data class LeftOverSystemInfo(
        val account: String,
        val userBean: JsonObject,
        val workerName: String = "",
        val staffNo: String = "",
        val staffName: String = ""
)